<template>
	<view class="address-page">
		<view v-if="addresses.length===0" class="empty">暂无收货地址</view>
		<view v-else class="list">
			<view class="card" v-for="addr in addresses" :key="addr.id" @click="onChoose(addr)">
				<view class="tag" v-if="addr.isDefault === 1">默认</view>
				<view class="info">
					<view class="row">
						<text class="name">{{ addr.receiver }}</text>
						<text class="phone">{{ addr.phone }}</text>
					</view>
					<text class="detail">{{ formatAddress(addr) }}</text>
				</view>
				<view class="ops">
					<button class="op" @click="edit(addr)">编辑</button>
					<button class="op primary" :disabled="addr.isDefault === 1" @click="setDefault(addr)">设为默认</button>
				</view>
			</view>
		</view>

		<view class="footer">
			<button class="add" @click="add">添加收货地址</button>
		</view>
	</view>
</template>

<script>
import { api } from '../../utils/request.js'

export default {
	data(){ return { addresses: [], chooseMode:false } },
	onLoad(query){ this.chooseMode = String(query?.choose||'')==='1'; },
	onShow(){ this.load(); },
	methods:{
		async load(){
			try{
				const r = await api.getAddresses();
				if(r.code===0){ this.addresses = r.data || []; }
			}catch{}
		},
		formatAddress(a){ return `${a.province || ''}${a.city || ''}${a.district || ''}${a.detail || ''}`; },
		edit(addr){
			const data = encodeURIComponent(JSON.stringify(addr));
			uni.navigateTo({ url: `/pages/address/edit?data=${data}` });
		},
		onChoose(addr){
			if(!this.chooseMode) return;
			const pages = getCurrentPages();
			const prev = pages[pages.length - 2];
			if(prev){
				const ec = this.$scope.getOpenerEventChannel && this.$scope.getOpenerEventChannel();
				if(ec){ ec.emit('addressChosen', addr); }
			}
			uni.navigateBack();
		},
		async setDefault(addr){
			try{
				const r = await api.setDefaultAddress(addr.id);
				if(r.code===0){ uni.showToast({ title:'已设为默认' }); this.load(); }
				else{ uni.showToast({ icon:'none', title:r.message||'设置失败' }); }
			}catch{ uni.showToast({ icon:'none', title:'请求失败' }); }
		},
		add(){
			uni.navigateTo({ url:'/pages/address/edit' });
		}
	}
}
</script>

<style>
.address-page{ padding: 16rpx 16rpx 140rpx; }
.empty{ text-align:center; color:#888; padding: 40rpx 0; }
.list{ display:flex; flex-direction: column; gap: 16rpx; }
.card{ position:relative; background:#fff; border-radius: 18rpx; padding: 24rpx; box-shadow: 0 8rpx 24rpx rgba(0,0,0,.06); }
.tag{ position:absolute; top:0; left:0; background:#ff9a45; color:#fff; padding: 6rpx 18rpx; border-top-left-radius:18rpx; border-bottom-right-radius:18rpx; font-size: 22rpx; }
.info{ margin-bottom: 16rpx; padding-top: 18rpx; }
.row{ display:flex; justify-content: space-between; align-items:center; margin-bottom: 8rpx; }
.name{ font-size: 30rpx; font-weight: 600; color:#333; }
.phone{ font-size: 28rpx; color:#666; }
.detail{ font-size: 26rpx; color:#666; line-height: 1.5; }
.ops{ display:flex; gap: 20rpx; }
.op{ flex:1; padding: 14rpx 0; border-radius: 999rpx; border: 2rpx solid #ddd; background:#fff; font-size: 26rpx; color:#333; }
.op.primary{ border-color:#ff7a45; color:#ff7a45; }
.op:disabled{ opacity:.5; }
.footer{ position: fixed; left:0; right:0; bottom: env(safe-area-inset-bottom); padding: 20rpx 16rpx calc(20rpx + env(safe-area-inset-bottom)); background: rgba(255,255,255,.96); box-shadow: 0 -6rpx 20rpx rgba(0,0,0,.05); display:flex; justify-content:center; }
.add{ width: 80%; background: linear-gradient(135deg,#39c5bb,#54e0c7); color:#fff; border-radius: 999rpx; padding: 20rpx 0; font-size: 30rpx; }
</style>

